"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const maishu_chitu_service_1 = require("maishu-chitu-service");
// import { config } from "../config";
const errors_1 = require("../errors");
let service = new maishu_chitu_service_1.Service();
let miniProgram = {
    appId: "wxb6238f6a8f843b2e",
    secret: "4f3877061a4daf7600900e227a4745b1"
};
exports.auth = {
    code2Session: async (code) => {
        if (!code)
            throw errors_1.errors.argumentNull("code");
        let url = `https://api.weixin.qq.com/sns/jscode2session`;
        let args = {
            appid: miniProgram.appId,
            secret: miniProgram.secret,
            js_code: code,
            grant_type: "authorization_code"
        };
        return get(url, args);
    }
};
async function get(url, args) {
    let r = await service.get(url, args);
    if (typeof r == "string") {
        r = JSON.parse(r);
    }
    let err = r;
    if (err.errcode != null && err.errmsg != null) {
        let error = { name: err.errcode, message: err.errmsg };
        return Promise.reject(error);
    }
    return r;
}
